$(document).ready(function () {
    $('#outbound-order-no').focus();

    $('.confirm-btn').on("click", function () {
        const value = $('.input').val();
        initByInput(value);
        selectByOutboundCode();
    });
    isOutbound();
});

//分拣称重
function sortWeightCallBack(data, cancel, opt) {
    if (!data) {
        return;
    }
    console.log("称重响应："+JSON.stringify(data));
    if (!cancel) {
        delResponseFromWcs(data, opt);
        //todo 逻辑处理
        const obj = data.data;
        if (obj.code == 0) {
        } else {
            BootstrapDialog.alert(data.msg);
        }
    }
}

//发送指令响应回调
function wcsToWmsResponseCallBack(data, cancel, opt) {
    if (!data) {
        return;
    }
    if (cancel) {
        //todo 隐藏操作按钮等之类的
    } else {
        console.log("000:" + JSON.stringify(data));
        delResponseFromWcs(data, opt);
        const obj = data.data;
        if (obj.code == 0) {
            inBufferPos(obj, true);
        } else {
            BootstrapDialog.confirm({
                title: '信息提示',
                message: '响应异常，是否继续?',
                btnCancelLabel: '取消', // <-- Default value is 'Cancel',
                btnOKLabel: '继续', // <-- Default value is 'OK',
                callback: function (result) {
                    // result will be true if button was click, while it will be false if users close the dialog directly.
                    if (result) {
                        outbound(null);
                    } else {
                        outboundCount();
                    }
                }
            });
        }
    }
}


//入库到位
function storageDoneCallBack(data, cancel) {
    if (!cancel) {
        const obj = data.data;
        if (obj.code == 1) {
            BootstrapDialog.alert(obj.msg);
        } else {
            storageDoneData(data);
        }
    }
}

function storageDoneData(data) {

}


//回调处理
function weighRespCallBack(data, cancel, opt) {
    if (!data) {
        return;
    }
    if (!cancel) {
        //todo 逻辑处理
        const obj = data.data;
        delResponseFromWcs(data, opt);
        if (obj.code == 0) {
        } else {
            BootstrapDialog.alert(obj.msg);
        }
    }
}

function inBufferPosCallBack(data, cancel, opt) {
    if (!data) {
        return;
    }
    if (!cancel) {
        const obj = data.data;
        if (obj.code == 1) {
            BootstrapDialog.alert(obj.msg);
        } else {
            delResponseFromWcs(data, opt);
            inBufferPos(obj, false);
        }
    }
}

//货箱离开库位
function leaveBufferPosCallBack(data, cancel, opt) {
    if (!data) {
        return;
    }
    console.log("入库离开缓存位响应：" + JSON.stringify(data));
    if (!cancel) {
        delResponseFromWcs(data, opt);
        const obj = data.data;
        if (obj.code == 0) {
        } else {
            BootstrapDialog.alert(obj.msg);
        }
    }
}

//出库到位，数据展示
function inBufferPos(data, response) {
    let total = 0, remainder = 0;
    for (let i = 0; i < data.data.length; i++) {
        const obj = data.data[i];
        switch (obj.status) {
            case "0":
                $('#number-waiting').text(obj.count);
                remainder = obj.count;
                break;
            case "1":
                $('#number-doing').text(obj.count);
                break;
            case "2":
                $('#number-done').text(obj.count);
                break;
        }
        total += obj.count;
    }
    $('#number-total').text(total);
    if (response && remainder > 0) {
        //todo 加个是否带箱出库
        //wcs响应成功，则延时一秒再下，缓解下频率
        setTimeout(function () {
            //还有待出库，则继续出库
            outboundDetail(null);
        }, 1000);
    } else if (!response && remainder <= 0) {
        //出库到位，且剩余箱数为0，则出库完成
        clearOutbound();
        $('#ware-box-code').val("");
        $('#good-box-code').val("");
    }
}

//校验当前用户是否有出库单
function isOutbound() {
    const opt = $("#form-param").serializeJSON();
    $.ajax({
        type: "POST",
        url: "/wcs/out/selectOutboundCodeByClient",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(opt),
        dataType: "json",
        success: function (data) {
            if (data.code == 0) {
                $('#outbound-input').addClass('hidden');
                $('#outbound-info').removeClass('hidden');
                $('#policy-outbound').val(data.data);
                selectByOutboundCode();
            } else {
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            BootstrapDialog.alert({
                title: "信息提示！",
                closable: false,
                message: XMLHttpRequest.responseJSON.msg
            });
        }
    });
}


function clearOutbound() {
    const opt = $("#form-param").serializeJSON();
    $.ajax({
        type: "POST",
        url: "/wcs/out/clearOutboundCodeByClient",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(opt),
        dataType: "json",
        success: function (data) {
            if (data.code == 0) {
                $('#outbound-info').addClass('hidden');
                $('#outbound-input').removeClass('hidden');
                $('#policy-outbound').val("");
            } else {
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            BootstrapDialog.alert({
                title: "信息提示！",
                closable: false,
                message: XMLHttpRequest.responseJSON.msg
            });
        }
    });
}

function selectByOutboundCode() {
    const opt = $("#form-param").serializeJSON();
    $.ajax({
        type: "POST",
        url: "/wcs/out/selectByCode",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(opt),
        dataType: "json",
        success: function (data) {
            if (data.code == 0) {
                outboundDetail(data);
            } else {
                BootstrapDialog.show({
                    size: BootstrapDialog.SIZE_NORMAL,
                    message: data.msg,
                    closable: false,
                    onshown: function (dialogRef) {
                        setTimeout(function () {
                            dialogRef.close();
                        }, 3000);
                        clearInput("outbound-order-no");
                    },
                    buttons: [{
                        label: '确定',
                        cssClass: 'btn-primary',
                        action: function (dialog) {
                            dialog.close();
                            clearInput("outbound-order-no");
                        }
                    }]
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            BootstrapDialog.alert({
                title: "信息提示！",
                closable: false,
                message: XMLHttpRequest.responseJSON.msg
            });
        }
    });
}

//根据取货单查询
function outboundDetail(data) {
    const opt = $("#form-param").serializeJSON();
    $.ajax({
        type: "POST",
        url: "/wcs/out/detail/selectByCode",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(opt),
        dataType: "json",
        success: function (data) {
            if (data.code == 0) {
                console.log("-----"+JSON.stringify(data));
                const code = data.data.boxCode;
                $('#ware-box-code').val(code);
                $('#good-box-code').val(code);
                outbound(data);
            } else {
                BootstrapDialog.show({
                    size: BootstrapDialog.SIZE_NORMAL,
                    message: data.msg,
                    closable: false,
                    onshown: function (dialogRef) {
                        setTimeout(function () {
                            dialogRef.close();
                        }, 3000);
                        clearInput("outbound-order-no");
                    },
                    buttons: [{
                        label: '确定',
                        cssClass: 'btn-primary',
                        action: function (dialog) {
                            dialog.close();
                            clearInput("outbound-order-no");
                        }
                    }]
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            BootstrapDialog.alert({
                title: "信息提示！",
                closable: false,
                message: XMLHttpRequest.responseJSON.msg
            });
        }
    });
}

function outbound(data) {
    const opt = $("#form-param").serializeJSON();
    $.ajax({
        type: "POST",
        url: "/wcs/notEntryByBoxCode",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(opt),
        dataType: "json",
        success: function (data) {
            if (data.code == 0) {
                //发送指令成功，还不知道要干啥
                $('#outbound-input').addClass('hidden');
                $('#outbound-info').removeClass('hidden');
            } else {
                BootstrapDialog.show({
                    size: BootstrapDialog.SIZE_NORMAL,
                    message: data.msg,
                    closable: false,
                    onshown: function (dialogRef) {
                        setTimeout(function () {
                            dialogRef.close();
                        }, 3000);
                        clearInput("outbound-order-no");
                    },
                    buttons: [{
                        label: '确定',
                        cssClass: 'btn-primary',
                        action: function (dialog) {
                            dialog.close();
                            clearInput("outbound-order-no");
                        }
                    }]
                });
            }
            outboundCount();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            BootstrapDialog.alert({
                title: "信息提示！",
                closable: false,
                message: XMLHttpRequest.responseJSON.msg
            });
        }
    });
}

//出库统计
function outboundCount() {
    const opt = $("#form-param").serializeJSON();
    $.ajax({
        type: "POST",
        url: "/wcs/out/detail/outboundCount",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(opt),
        dataType: "json",
        success: function (data) {
            if (data.code == 0) {
                inBufferPos(data, false);
            } else {
                BootstrapDialog.show({
                    size: BootstrapDialog.SIZE_NORMAL,
                    message: data.msg,
                    closable: false,
                    onshown: function (dialogRef) {
                        setTimeout(function () {
                            dialogRef.close();
                        }, 3000);
                        clearInput("outbound-order-no");
                    },
                    buttons: [{
                        label: '确定',
                        cssClass: 'btn-primary',
                        action: function (dialog) {
                            dialog.close();
                            clearInput("outbound-order-no");
                        }
                    }]
                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            BootstrapDialog.alert({
                title: "信息提示！",
                closable: false,
                message: XMLHttpRequest.responseJSON.msg
            });
        }
    });
}